home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / tpjr.com / JOYDEMO.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-10-22  |  10.0 KB  |  339 lines

  1. program JoyDemo;
  2.  
  3. {
  4. Copyright (c) 1989, 1990 David B. Howorth
  5.  
  6. Requires Turbo Pascal 5.0 or later.
  7.  
  8. Program last revised May 9, 1989.
  9. This comment last revised October 22, 1990.
  10.  
  11. This program demonstrates the procedures and functions in JOYSTICK.TPU.
  12. NOTE that before this demonstration can work JOYSTICK.TPU must be compiled
  13. to disk from the accompanying file JOYSTICK.PAS.
  14.  
  15. Permission is granted to distribute this file and the accompanying files
  16. (JOYSTICK.PAS and JOYSTICK.DOC) provided (1) all three files are distributed
  17. together and (2) no fee is charged.
  18.  
  19. Permission is granted to include compiled versions of the routines in these
  20. files in any program, commercial or noncommercial, provided only that if the
  21. program is distributed, whether commercially or noncommercially, a copy
  22. (including any documentation) be sent to the author; if you distribute your
  23. program as shareware, treat me as a registered user.  My address is
  24. 01960 SW Palatine Hill Road, Portland, Oregon 97219.
  25. }
  26.  
  27. Uses Dos, Crt, Joystick;
  28.  
  29. const
  30.   header = '                         JOYSTICK DEMONSTRATION NUMBER ';
  31.   footer ='                   Press a joystick button to end demonstration.';
  32.   DemoChar = #22;
  33.  
  34. type
  35.   CursorType = (none,normal,fat);
  36.  
  37. var
  38.   InitX, InitY : word;
  39.   { These variables are initialized at start up to get the coordinates of
  40.     Joystick A when it is centered.  If Joystick A is not centered at
  41.     start up, Demo3 may not work correctly. }
  42.  
  43.   NumberOfSticks : byte;
  44.  
  45.   Reg : Registers;
  46.  
  47.   CursorStartLine, CursorEndLine : byte;
  48.  
  49. {--------------------------------------}
  50.  
  51. procedure GetCursorData;
  52. begin
  53.   Reg.ah := $0F;
  54.   intr($10,Reg);
  55.   Reg.ah := $03;
  56.   intr($10,Reg);
  57.   CursorStartLine := Reg.ch;
  58.   CursorEndLine := Reg.cl;
  59. end;
  60.  
  61. {--------------------------------------}
  62.  
  63. procedure SetCursor(c : CursorType);
  64. begin
  65.   case c of
  66.     none : Reg.ch := $20;
  67.     normal : Reg.ch := CursorStartLine;
  68.     fat : Reg.ch := 0;
  69.   end;
  70.   Reg.cl := CursorEndLine;
  71.   Reg.ah := 1;
  72.   intr($10,Reg);
  73. end;
  74.  
  75. {--------------------------------------}
  76.  
  77. procedure DrawBox(x1,y1,x2,y2 : integer);
  78. var
  79.   i : byte;
  80. begin
  81.   gotoxy(x1,y1);
  82.   write(#218);
  83.   for i := x1 + 1 to x2 - 1 do write(#196);
  84.   write(#191);
  85.   for i := y1 + 1 to y2 - 1 do
  86.   begin
  87.     gotoxy(x1,i);
  88.     write(#179);
  89.     gotoxy(x2,i);
  90.     write(#179);
  91.   end;
  92.   gotoxy(x1,y2);
  93.   write(#192);
  94.   for i := x1 + 1 to x2 - 1 do write(#196);
  95.   write(#217);
  96. end;
  97.  
  98. {--------------------------------------}
  99.  
  100. procedure Demo1;
  101. var  ch : char;
  102. begin
  103.   clrscr;
  104.   writeln(header,'1');
  105.   gotoxy(1,7);
  106.   writeln
  107.   ('This is just a test of the function JoystickPresent, which indicates,');
  108.   writeln('as you might guess, whether a joystick is present.');
  109.   writeln;
  110.   if JoystickPresent
  111.     then writeln('You have one.')
  112.     else writeln('You don''t have one attached.');
  113.   writeln;
  114.   writeln;
  115.   write('Caveat:  See section F.2. of JOYSTICK.DOC.');
  116.   gotoxy(14,23);
  117.   write('How many joysticks do you have installed?  ');
  118.   repeat
  119.     ch := readkey;
  120.   until ch in ['0'..'2'];
  121.   NumberOfSticks := ord(ch) - 48;
  122. end;
  123.  
  124. {--------------------------------------}
  125.  
  126. procedure Demo2;
  127.  
  128. { This demonstates both ReadJoy, which reads the joystick X and Y coordinates
  129.   and the functions ButtonA1, etc., which read the 4 joystick buttons.
  130.   Outputs raw data to the screen. }
  131.  
  132. const
  133.   line1a = 'JOYSTICK A';
  134.   line1b = '                                 JOYSTICK B';
  135.   line2a = 'X-Axis  Y-Axis  Button 1  Button 2';
  136.   line2b = '         X-Axis  Y-Axis  Button 1  Button 2';
  137.  
  138. type
  139.   StatusStringType = string[3];
  140.  
  141. var
  142.   JoyAX, JoyAY, JoyBX, JoyBY : word;
  143.   line : string;
  144.   ch : char;
  145.  
  146. {-------}
  147.  
  148.   function ButtonStatusSt(b : boolean) : StatusStringType;
  149.   begin
  150.     if b then ButtonStatusSt := 'IN' else ButtonStatusSt := 'OUT';
  151.   end;
  152.  
  153. {-------}
  154.  
  155. begin
  156.   clrscr;
  157.   SetCursor(none);
  158.   writeln(header,'2');
  159.   writeln;
  160.   writeln('                        (Joystick input shown as raw data)');
  161.   if (NumberOfSticks = 2)
  162.     then line := line1a + line1b
  163.     else line := line1a;
  164.   gotoxy(40 - (length(line) div 2),7);
  165.   writeln(line);
  166.   if (NumberOfSticks = 2)
  167.     then line := line2a + line2b
  168.     else line := line2a;
  169.   gotoxy(40 - (length(line) div 2),8);
  170.   write(line);
  171.   gotoxy(24,23);
  172.   write('To go on to next demo, press a key.');
  173.  
  174.   repeat
  175.     ReadJoyA(JoyAX,JoyAY);
  176.     if (NumberOfSticks = 2)
  177.       then gotoxy(1,9)
  178.       else gotoxy(22,9);
  179.     write(JoyAX:5,JoyAY:8,
  180.           ButtonStatusSt(ButtonA1):9,ButtonStatusSt(ButtonA2):10);
  181.     if (NumberOfSticks = 2)
  182.       then begin
  183.              ReadJoyB(JoyBX,JoyBY);
  184.              write(JoyBX:16,JoyBY:8,
  185.                    ButtonStatusSt(ButtonB1):9,ButtonStatusSt(ButtonB2):10);
  186.            end;
  187.   until keypressed;
  188.   while keypressed do ch := readkey;  { clear key buffer }
  189. end; { Demo2 }
  190.  
  191. {--------------------------------------}
  192.  
  193. procedure Demo3;
  194.   { Shows how to use the joystick as a DIRECTION indicator. }
  195.  
  196. var
  197.   JoyX, JoyY       : word;
  198.   ScreenX, ScreenY : byte;
  199.  
  200. begin
  201.   clrscr;
  202.   SetCursor(none);
  203.   writeln(header,'3');
  204.   writeln;
  205.   writeln('                        (Joystick as direction indicator)');
  206.   gotoxy(1,25);
  207.   write(footer);
  208.   DrawBox(1,5,80,24);
  209.   ScreenX := 40;                  { Initial coordinates of character }
  210.   ScreenY := 14;                  { in center of the box.            }
  211.  
  212.   repeat
  213.     gotoxy(ScreenX,ScreenY);
  214.     write(' ');                       { erase previous character }
  215.     ReadJoyA(JoyX,JoyY);
  216.     if JoyX > InitX + (InitX div 5)
  217.       then inc(ScreenX)
  218.       else if JoyX < InitX - (InitX div 5)
  219.              then dec(ScreenX);
  220.     if JoyY > InitY + (InitY div 5)
  221.       then inc(ScreenY)
  222.       else if JoyY < InitY - (InitY div 5)
  223.              then dec(ScreenY);
  224.  
  225. { If all you are interested in is what direction the joystick has been moved,
  226.   all you need to do is compare the current position with the initial
  227.   position calibrated by the program.  If you want your program to be
  228.   relatively portable, you ought to make the comparison in as relative terms
  229.   as possible, i.e., use something like 'if JoyX > InitX + (InitX div 5)',
  230.   rather than something like 'if JoyX > InitX + 10'.  It's possible the
  231.   latter expression won't work effectively with some makes of joystick or at
  232.   the speed of some chips.                                                  }
  233.  
  234.     if ScreenX > 79 then ScreenX := 79;        { make sure      }
  235.     if ScreenX < 2 then ScreenX := 2;          { the character  }
  236.     if ScreenY > 23 then ScreenY := 23;        { doesn't get    }
  237.     if ScreenY < 6 then ScreenY := 6;          { out of the box }
  238.     gotoxy(ScreenX,ScreenY);
  239.     write(DemoChar);
  240.     delay(30);                         { slow things down a bit }
  241.   until ButtonA1 or ButtonA2;
  242.   repeat until not (ButtonA1 or ButtonA2); { wait till button no longer in }
  243. end; { Demo3 }
  244.  
  245. {--------------------------------------}
  246.  
  247. procedure Demo4;
  248. { Shows how to use the joystick as a POSITION indicator. }
  249.  
  250. var
  251.   LowX,      { Minimum and          }
  252.   LowY,      { maximum joystick     }
  253.   HighX,     { coordinates,read     }
  254.   HighY,     { once, at calibration.}
  255.  
  256.   JoyX,      { Current joystick coordinates, }
  257.   JoyY       { read repeatedly.              }
  258.  
  259.              : word;
  260.  
  261.   ScreenX,   { Screen coordinates of cursor, }
  262.   ScreenY,   { derived from JoyX and JoyY.   }
  263.   OldScreenX,
  264.   OldScreenY : byte;
  265.  
  266. begin
  267.   clrscr;
  268.   writeln(header,'4');
  269.   writeln;
  270.   writeln('                         (Joystick as position indicator)');
  271.   gotoxy(1,7);
  272.   writeln('You may want to try this demo with the joystick unlocked.');
  273.  
  274.   { Calibrate joystick: }
  275.   gotoxy(1,9);
  276.   repeat
  277.    writeln
  278.    ('Move joystick to upper-left corner and press one of its buttons.');
  279.    repeat until ButtonA1 or ButtonA2;
  280.    ReadJoyA(LowX,LowY);
  281.    repeat until not (ButtonA1 or ButtonA2); { wait till button no longer in }
  282.    writeln
  283.    ('Move joystick to lower-right corner and press one of its buttons.');
  284.    repeat until ButtonA1 or ButtonA2;
  285.    ReadJoyA(HighX,HighY);
  286.    repeat until not (ButtonA1 or ButtonA2); { wait till button no longer in }
  287.    if (LowX >= HighX) or (LowY >= HighY)
  288.      then begin
  289.             writeln;
  290.             writeln('You did not calibrate correctly.  Please recalibrate.');
  291.             writeln;
  292.           end;
  293.   until (LowX < HighX) and (LowY < HighY);
  294.   clrscr;
  295.   SetCursor(none);
  296.   DrawBox(1,1,80,24);
  297.   gotoxy(1,25);
  298.   write(footer);
  299.   OldScreenX := 100; OldScreenY := 100; { so first erasure will be ignored }
  300.   repeat
  301.     ReadJoyA(JoyX,JoyY);
  302.  
  303.     if JoyX < LowX then JoyX := LowX;  { If joystick was not at extreme     }
  304.     if JoyX > HighX then JoyX := HighX;{ positions during calibration,      }
  305.     if JoyY < LowY then JoyY := LowY;  { screen coordinates may lie off-    }
  306.     if JoyY > HighY then JoyY := HighY;{ screen.  These statements fix that.}
  307.  
  308.     ScreenX := (((JoyX - LowX + 1) * 77) div (HighX - LowX)) + 2;
  309.     ScreenY := (((JoyY - LowY + 1) * 21) div (HighY - LowY)) + 2;
  310.     { The above formulas simply calculate a position on the screen for the
  311.       cursor. }
  312.  
  313.     if (OldScreenX <> ScreenX) or (OldScreenY <> ScreenY)
  314.       then begin
  315.              gotoxy(OldScreenX,OldScreenY);write(' ');
  316.              gotoxy(ScreenX,ScreenY);write(DemoChar);
  317.              OldScreenX := ScreenX; OldScreenY := ScreenY;
  318.            end;
  319.     gotoxy(1,1);  { <-- Don't know why, but this seems to make this demo a }
  320.                   {     little less fluttery.  Should be unneccessary with }
  321.                   {     programs that do more than this one.               }
  322.   until ButtonA1 or ButtonA2;
  323. end; { Demo4 }
  324.  
  325. begin { main program }
  326.   ReadJoyA(InitX,InitY);
  327.   { Calibrate joystick.  These values will be used as the base values for
  328.     Demo3.  The demo assumes that at startup Joystick A will be centered.
  329.     If it is not, Demo3 will probably not work correctly. }
  330.   GetCursorData;
  331.   Demo1;
  332.   if (NumberOfSticks = 0) then halt;
  333.   Demo2;
  334.   Demo3;
  335.   Demo4;
  336.   clrscr;
  337.   SetCursor(normal);
  338. end.
  339.